// DriveNotes. Copyright (c) 2011, Andrei Senchuk. All rights reserved.
package net.taviscaron.drivenotes.web.util;

import java.util.Map;
import net.taviscaron.drivenotes.shared.model.entity.Car;

/**
 * Param utils
 * @author Andrei Senchuk
 */
public class ParamUtils {
    public static Long parseLongParam(Map params, String name, String prefix) {
        String[] values = (String[]) params.get(prefix + name);
        if(values != null) {
            String val = values[0];
            return (val != null && !val.isEmpty()) ? Long.valueOf(val) : null;
        } else {
            return null;
        }
    }

    public static Integer parseIntParam(Map params, String name, String prefix) {
        String[] values = (String[]) params.get(prefix + name);
        if(values != null) {
            String val = values[0];
            return (val != null && !val.isEmpty()) ? Integer.valueOf(val) : null;
        } else {
            return null;
        }
    }

    public static Float parseFloatParam(Map params, String name, String prefix) {
        String[] values = (String[]) params.get(prefix + name);
        if(values != null) {
            String val = values[0];
            return (val != null && !val.isEmpty()) ? Float.valueOf(val) : null;
        } else {
            return null;
        }
    }

    public static Double parseDoubleParam(Map params, String name, String prefix) {
        String[] values = (String[]) params.get(prefix + name);
        if(values != null) {
            String val = values[0];
            return (val != null && !val.isEmpty()) ? Double.valueOf(val) : null;
        } else {
            return null;
        }
    }

    public static Boolean parseBoolParam(Map params, String name, String prefix) {
        String[] values = (String[]) params.get(prefix + name);
        if(values != null) {
            String val = values[0];
            return (val != null && !val.isEmpty()) ? Boolean.valueOf(val) : null;
        } else {
            return null;
        }
    }

    public static Car.EngineType parseEngineTypeParam(Map params, String name, String prefix) {
        String[] values = (String[]) params.get(prefix + name);
        if(values != null) {
            String val = values[0];
            return (val != null && !val.isEmpty()) ? Car.EngineType.valueOf(val.toUpperCase()) : null;
        } else {
            return null;
        }
    }

    public static String parseStringParam(Map params, String name, String prefix) {
        String[] values = (String[]) params.get(prefix + name);
        if(values != null) {
            String val = values[0];
            return (val != null && !val.isEmpty()) ? val : null;
        } else {
            return null;
        }
    }
}
